/*
 * version date author 
 * 1.0  Nov 28, 2012 Neal Miao 
 * 
 * Copyright(c) 2012, by HTWater. All Rights Reserved.
 */
package cn.miao.framework.action;

import java.sql.Timestamp;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import net.sf.json.JsonConfig;
import cn.miao.framework.entity.Requester;
import cn.miao.framework.entity.Responser;
import cn.miao.framework.util.JsonDateProcessor;
import cn.miao.framework.util.XmlUtil;

/**
 * 所有Action的基类
 * 
 * @author Neal Miao
 * @version
 * @since v 1.0
 * @Date Nov 28, 2012 10:47:39 AM
 * 
 * @see
 */
public class DoAction {
	
	public Responser responser = new Responser();
	public Requester params;
	public HttpSession session;
	public HttpServletRequest request;
	public final static String JSON = "json";
	public final static String XML = "xml";
	public final static String TEXT = "html";
	public final static String REDIRECT = "redirect";
	public final static String NONE = "none";
	
	public HttpServletRequest getRequest() {
		return request;
	}

	public void setRequest(HttpServletRequest request) {
		this.request = request;
	}
	
	/**
	 * 设置参数
	 * 
	 * @param mySession
	 * @return void
	 * @since v 1.0
	 */
	public void setParam(Requester params) {
		this.params = params;
	}

	/**
	 * 用于传递session
	 * 
	 * @param mySession
	 * @return void
	 * @since v 1.0
	 */
	public void setSession(HttpSession mySession) {
		if (null == session) {
			session = mySession;
		}
	}

	/**
	 * 转对象Json
	 * 
	 * @param object
	 * @return String
	 * @since v 1.0
	 */
	public String parseJSON(Object object) {
		JsonConfig jsonConfig = new JsonConfig();
		jsonConfig.registerJsonValueProcessor(Date.class, new JsonDateProcessor());
		jsonConfig.registerJsonValueProcessor(java.sql.Date.class, new JsonDateProcessor());
		jsonConfig.registerJsonValueProcessor(Timestamp.class, new JsonDateProcessor());
		if (object instanceof Map<?, ?>) {
			return JSONObject.fromObject(object, jsonConfig).toString();
		} else if (object instanceof List<?>) {
			return JSONArray.fromObject(object, jsonConfig).toString();
		} else {
			return JSONObject.fromObject(object, jsonConfig).toString();
		}
	}
	
	/**
	 * 转对象Json  带时间字符串的
	 * 
	 * @param object
	 * @param dateFormat 时间字符串格式
	 * @return String
	 * @since v 1.0
	 */
	public String parseJSON(Object object, String dateFormat) {
		JsonConfig jsonConfig = new JsonConfig();
		jsonConfig.registerJsonValueProcessor(Date.class, new JsonDateProcessor(dateFormat));
		jsonConfig.registerJsonValueProcessor(java.sql.Date.class, new JsonDateProcessor(dateFormat));
		jsonConfig.registerJsonValueProcessor(Timestamp.class, new JsonDateProcessor(dateFormat));
		if (object instanceof Map<?, ?>) {
			return JSONObject.fromObject(object, jsonConfig).toString();
		} else if (object instanceof List<?>) {
			return JSONArray.fromObject(object, jsonConfig).toString();
		} else {
			return JSONObject.fromObject(object, jsonConfig).toString();
		}
	}
	
	/**
	 * 转对xml
	 * 
	 * @param object
	 * @return String
	 * @since v 1.0
	 */
	public String parseXML(Object object) {
		if (object instanceof Map<?, ?>) {
			return XmlUtil.maptoXml((Map<?, ?>)object);
		} else if (object instanceof List<?>) {
			return XmlUtil.listtoXml((List<?>)object);
		} else {
			return "";
		}
	}
}
